package com.focusmes.modules.base.factory.component;


import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.online.cgform.enhance.CgformEnhanceJavaListInter;
import org.jeecg.modules.online.cgform.mapper.OnlCgformFieldMapper;
import org.jeecg.modules.online.config.exception.BusinessException;
import org.springframework.stereotype.Component;
import sun.security.smartcardio.SunPCSC;

import java.util.*;

@Component("cgformEnhanceQueryDemo")
public class CgformEnhanceQueryDemo implements CgformEnhanceJavaListInter {

    @Override
    public void execute(String tableName, List<Map<String, Object>> data) throws BusinessException {
        //List<VirtualDict> dict = virtualDictData();

        List<Map<String, Object>> listMaps = MesFactoryData();

        for (Map<String, Object> map : data) {

            Object db = map.get("factory_id");
            System.out.println("+++++++++++++++++++++++++++++++++++++++++");
            System.out.println(db);
            if(db==null){
                continue;
            }
//            String text = dict.stream()
//                    .filter(p -> db.toString().equals(p.getValue()))
//                    .map(VirtualDict::getText)
//                    .findAny()
//                    .orElse("");
//            map.put("factory_id",text);
            for (Map<String, Object> lmap : listMaps) {
                for (Map.Entry<String, Object> m : lmap.entrySet()) {
//                    System.out.print(m.getKey() + "    ");
//                    System.out.println(m.getValue());
                    if(m.getKey().equals(db)){
                        map.put("factory_id",m.getValue());
                    }
                }
            }
        }

//        OnlCgformFieldMapper onlCgformFieldMapper = SpringContextUtils.getBean(OnlCgformFieldMapper.class);
//        Map<String,Object> params = new HashMap<>();
//        String sql = "update "+ tableName+" set date=#{date,jdbcType=DATE} where id=#{id,jdbcType=VARCHAR}";
//        params.put("execute_sql_string",sql);
//        onlCgformFieldMapper.executeUpdatetSQL(params);
//        System.out.println(("----- 更新 method test 工厂表 ------"));

//        OnlCgformFieldMapper onlCgformFieldMapper = SpringContextUtils.getBean(OnlCgformFieldMapper.class);
//        String sqlStr = "select * from "+ tableName+" ";
//        System.out.println(onlCgformFieldMapper.queryListBySql(sqlStr));

//        System.out.println(listMaps);
//
//        for (Map<String, Object> map : listMaps) {
//            for (String s : map.keySet()) {
//                System.out.print(map.get(s) + "  ");
//            }
//        }
//        System.out.println();
//        System.out.println("========================");
//        for (int i = 0; i < listMaps.size(); i++) {
//            Map<String, Object> map = listMaps.get(i);
//            Iterator iterator = map.keySet().iterator();
//            while (iterator.hasNext()) {
//                String string = (String) iterator.next();
//                System.out.println(map.get(string));
//            }
//        }
//        System.out.println("++++++++++++++++++++++++++++");
//        for (Map<String, Object> map : listMaps) {
//            for (Map.Entry<String, Object> m : map.entrySet()) {
//                System.out.print(m.getKey() + "    ");
//                System.out.println(m.getValue());
//            }
//        }
//        System.out.println("-----------------------------");

    }
    /**
     * 模拟字典数据
     * @return
     */
    private List<Map<String, Object>> MesFactoryData(){
        OnlCgformFieldMapper onlCgformFieldMapper = SpringContextUtils.getBean(OnlCgformFieldMapper.class);
        String sqlStr = "select * from mes_factory ";
        System.out.println(("----- method test 获取工厂列表 ------"));
        List<Map<String, Object>> list = onlCgformFieldMapper.queryListBySql(sqlStr);
        return list;
    }
    /**
     * 模拟字典数据
     * @return
     */
    private List<VirtualDict> virtualDictData(){
        List<VirtualDict> dict = new ArrayList<VirtualDict>();
        dict.add(new VirtualDict("1292375042437042178","北京1231"));
        dict.add(new VirtualDict("1292374825654439938","北京1232"));
        dict.add(new VirtualDict("1299360422034137090","北京1233"));
        return dict;
    }

    class VirtualDict {
        String value;
        String text;

        public VirtualDict(String value,String text){
            this.value = value;
            this.text = text;
        }

        public String getValue(){
            return value;
        }


        public String getText(){
            return text;
        }
    }
}